package com.bike.bikeserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bike.bikeserver.common.core.domain.CommonResult;
import com.bike.bikeserver.common.utils.StringUtils;
import com.bike.bikeserver.domain.Rewards;
import com.bike.bikeserver.domain.RewardsDetail;
import com.bike.bikeserver.domain.WXUser;
import com.bike.bikeserver.domain.vo.HistoryInfoVo;
import com.bike.bikeserver.mapper.RewardsDetailMapper;
import com.bike.bikeserver.mapper.RewardsMapper;
import com.bike.bikeserver.mapper.WXUserMapper;
import com.bike.bikeserver.service.IRewardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.bike.bikeserver.constant.Constants.REWARDS_UNGOT;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/7/11 15:33
 */
@Service
public class RewardsServiceImpl implements IRewardsService
{
    @Autowired
    RewardsMapper rewardsMapper;
    @Autowired
    WXUserMapper wxUserMapper;
    @Autowired
    RewardsDetailMapper rewardsDetailMapper;

    @Override
    public CommonResult getRewardsList()
    {
        return CommonResult.success(rewardsMapper.selectList(null));
    }

    @Override
    public CommonResult getRewardsForHistory()
    {
        List<Rewards> rewardsList = rewardsMapper.selectList(null);
        List<HistoryInfoVo> historyInfoVoList = new ArrayList<>();
        for (Rewards rewards : rewardsList)
        {
            HistoryInfoVo historyInfoVo = new HistoryInfoVo();
            historyInfoVo.setRewardsId(rewards.getId());
            historyInfoVo.setTime(rewards.getCreateTime());
            historyInfoVo.setName(rewards.getRewardsName());
            historyInfoVo.setInfo("限" + rewards.getValidLocation() + "|单笔抵" + rewards.getValidPeriod());
            historyInfoVo.setState("价值积分：" + String.valueOf(rewards.getRewardsValue()));
            historyInfoVoList.add(historyInfoVo);
        }
        return CommonResult.success(historyInfoVoList);
    }

    @Override
    public CommonResult exchangeRewardsByNickName(String nickName, Integer rewardsId)
    {
        // 根据id获取奖券对象信息
        Rewards rewards = rewardsMapper.selectById(rewardsId);
        // 根据nickName获取用户信息
        LambdaQueryWrapper<WXUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WXUser::getNickName, nickName);
        WXUser wxUser = wxUserMapper.selectOne(wrapper);
        if (wxUser.getScore() >= rewards.getRewardsValue())
        {
            // 减去积分数
            wxUser.setScore(wxUser.getScore() - rewards.getRewardsValue());
            wxUserMapper.updateById(wxUser);
            RewardsDetail rewardsDetail = new RewardsDetail();
            rewardsDetail.setRewardsName(rewards.getRewardsName());
            rewardsDetail.setNickName(nickName);
            rewardsDetail.setRewardsGetState(REWARDS_UNGOT);
            rewardsDetailMapper.insert(rewardsDetail);
            return CommonResult.success("兑换成功，可去【领取奖励】领取！");
        }
        else
        {
            return CommonResult.error("兑换失败！积分不足");
        }
    }

    @Override
    public CommonResult getRewardsLikeName(String rewardsName)
    {
        LambdaQueryWrapper<Rewards> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Rewards::getRewardsName, rewardsName);
        List<Rewards> rewards = rewardsMapper.selectList(wrapper);
        return CommonResult.success("查询成功！为您找到" + rewards.size() + "条记录！", rewards);
    }

    @Override
    public CommonResult editRewardsById(Rewards rewards)
    {
        // 查找是否重复修改
        Rewards rewards1 = rewardsMapper.selectById(rewards.getId());
        if (rewards.getRewardsValue() == rewards1.getRewardsValue() && rewards.getRewardsName().equals(rewards1.getRewardsName()) && rewards.getRewardsApplication().equals(rewards1.getRewardsApplication()) && rewards.getValidPeriod().equals(rewards1.getValidPeriod()) && rewards.getValidLocation().equals(rewards1.getValidLocation()))
        {
            return CommonResult.warn("请勿输入空值或重复修改！");
        }
        rewards1.setRewardsName(rewards.getRewardsName());
        rewards1.setRewardsValue(rewards.getRewardsValue());
        rewards1.setRewardsApplication(rewards.getRewardsApplication());
        rewards1.setValidLocation(rewards.getValidLocation());
        rewards1.setValidPeriod(rewards.getValidPeriod());
        // 判断时限是否满足格式：xx分钟，xx为正整数
        String regex = "^[1-9]\\d*分钟$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(rewards1.getValidPeriod());
        if (!matcher.matches())
        {
            return CommonResult.warn("单次有效时限格式为xx分钟，且xx为正整数！");
        }
        int update = rewardsMapper.updateById(rewards1);
        if (update != 0)
        {
            return CommonResult.success("修改成功！");
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult deleteRewardsById(Rewards rewards)
    {
        int count = rewardsMapper.deleteById(rewards.getId());
        if (count != 0)
        {
            return CommonResult.success("已成功删除！");
        }
        return CommonResult.error();
    }

    @Override
    public CommonResult addRewards(Rewards rewards)
    {
        // 判断空值
        if (StringUtils.isBlank(rewards.getRewardsName()) || StringUtils.isBlank(rewards.getValidLocation()) || StringUtils.isBlank(rewards.getRewardsApplication()) || StringUtils.isBlank(rewards.getValidPeriod()) || rewards.getRewardsValue() == 0)
        {
            return CommonResult.warn("输入的字段不可有空值！");
        }
        rewards.setCreateTime(new Date());
        rewards.setUpdateTime(new Date());
        // 判断时限是否满足格式：xx分钟，xx为正整数
        String regex = "^[1-9]\\d*分钟$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(rewards.getValidPeriod());
        if (!matcher.matches())
        {
            return CommonResult.warn("单次有效时限格式为xx分钟，且xx为正整数！");
        }
        int insert = rewardsMapper.insert(rewards);
        if (insert != 0)
        {
            return CommonResult.success("添加成功！");
        }
        return CommonResult.error();
    }
}
